package ltmt.test.main;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;

import com.ltmt.es.persistence.entity.HistoricCombination;
import com.ltmt.es.persistence.repository.PrimitivaHistoricCombinationRepository;
import com.ltmt.es.persistence.utils.SpringDataUtil;
import com.ltmt.es.utils.castor.PrimitivaCastor;
import com.ltmt.es.utils.csvparser.CSVUtils;

public class batchLoader {

	public static void main(String[] args) {
		

		try {
			
			JpaRepositoryFactory jpaRepositoryFactory = SpringDataUtil.getRepositoryFactory();
			PrimitivaHistoricCombinationRepository phcr = jpaRepositoryFactory
					.getRepository(PrimitivaHistoricCombinationRepository.class);
			
			phcr.count();
			
			CSVUtils pCSVUtils = new CSVUtils();
			List<String[]> listTemplateCSV = pCSVUtils.parseCSV("muestraAll.csv");
			
			HistoricCombination hComb;
			List<HistoricCombination> listHComb = new ArrayList<HistoricCombination>();
				
			
			for (String[] templateCSV : listTemplateCSV){
				hComb = PrimitivaCastor.toHistoricCombination(templateCSV);
				listHComb.add(hComb);
			}
			
			EntityManager em = SpringDataUtil.getEntityManager();	
			em.getTransaction().begin();
			phcr.save(listHComb);
			phcr.flush();
			
		}catch (Exception ex){
			ex.printStackTrace();
		}

	}

}
